import BaseLayer from "../Base/BaseLayer";
import GameUI from "../game/GameUI";
import { IMessage } from "../Interface/IMessage";
import EventID from "./EventID";

const { ccclass, property } = cc._decorator;

export interface TaskConfig {
    id: number;
    title: string;
    desc: string;
}

@ccclass
export default class TaskModule extends BaseLayer implements IMessage {
    public static instance: TaskModule = null;

    @property({ type: cc.Label, tooltip: "标题" })
    private labTitle: cc.Label = null;
    @property({ type: cc.Label, tooltip: "描述" })
    private labDesc: cc.Label = null;

    public readonly tasks: TaskConfig[] = [
        { id: 1, title: '打扫屋子', desc: '收拾垃圾/清理蜘蛛网/修理桌子' },
        { id: 2, title: '维修屋顶', desc: '前往矿村获得铁片，回家维修屋顶' },
        { id: 3, title: '获得启动资金', desc: '获取手镯，前往矿村大市场卖出' },
        { id: 4, title: '获得第一桶金', desc: '前往矿山获取符咒' },
        { id: 5, title: '为妈妈求医问药', desc: '前往御康馆刘神医处' },
        { id: 6, title: '成为医馆学徒', desc: '完成刘神医的要求成为药馆学徒' },
        { id: 7, title: '帮妈妈针灸治病', desc: '回到家中为妈妈针灸' },
        { id: 8, title: '积累资产', desc: '前往矿山获取五行珠' },
        { id: 9, title: '收购小卖店', desc: '前往小卖店，跟花大妈砍价' },
        { id: 10, title: '招聘店员', desc: '回家后筛选出合适的店员' },
        { id: 11, title: '找到失踪线索', desc: '回家后寻找犯人留下的线索。' },
        { id: 12, title: '寻找照片碎片', desc: '根据照片碎片的指引找到完整照片' },
    ];

    // private taskData: ITaskData = null;
    private callback: Function = null;

    recvMsg(cmd: number, data: any) {
        switch (cmd) {
            case EventID.REFRESH_TASK:
                {

                }
                break;
        }
    }

    protected start() {
        this.addMessage(EventID.REFRESH_TASK, this);

        TaskModule.instance = this;

        // this.taskData = StorageUtil.instance.getTaskData();
    }

    public showTask(callback: Function) {
        this.callback = callback;

        const config = this.getTaskConfig();
        if (!config) return;
        this.labTitle.string = config.title;
        this.labDesc.string = config.desc;

        this.node.stopAllActions();
        cc.tween(this.node).to(0.5, { x: 0 }).start();
        // cc.tween(this.node).to(0.5, { x: 0 }).delay(2).call(() => {
        //     this.hideTask();
        // }).start();
    }

    public hideTask() {
        this.node.stopAllActions();
        cc.tween(this.node).to(0.5, { x: -2500 }).call(() => {
            this.callback && this.callback();
            this.callback = null;
        }).start();
    }

    public getTaskConfig(): TaskConfig {
        const config = this.tasks.find((o) => o.id === GameUI.instance.taskData.id);
        return config;
    }

    public saveData() {
        GameUI.instance.taskData.id++;
        GameUI.instance.taskData.isAutoShow = 0;
        console.log("taskData:", GameUI.instance.taskData);
    }

    private async onBtnsClicked(event: cc.Event.EventTouch, eventData: string) {
        const name = event.target.name;
        switch (name) {
            case 'btnBack':
                {
                    this.hideTask();
                }
                break;
        }
    }
}